package com.hs.task.util;

/**
 * Created by chenyq5 on 2016-11-25.
 */

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JaxbDateSerializer extends XmlAdapter<String, Date> {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    private SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @Override
    public String marshal(Date date) throws Exception {
        return dateFormat.format(date);
    }

    @Override
    public Date unmarshal(String date) throws Exception {
        Date dt = new Date();
        try {
            dt = dateFormat1.parse(date);
        } catch (ParseException e) {
            dt = dateFormat.parse(date);
        }
        return dt;
    }

    public static void main(String[] agrs) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            Date dt = dateFormat1.parse("1990/11/01");
            int i = 1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}